﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Onyeyiri;

namespace iTunes_Media_Keys
{
    class HotKeyIniManager
    {
        IniFile ini;
        //
        string path;

        public HotKeyIniManager(string p)
        {
            path = p + "\\keys.ini";
            if (!File.Exists(path)) { File.Create(path); }
            ini = new IniFile(path);
        }

        public bool SaveHotKey(HotKeyProfile prof)
        {
            string section = "Function" + ((int)prof.Function).ToString();
            if (ini.GetString(section, "Modifier", "").Length > 0)
            {
                return false;
            }
            //
            ini.WriteValue(section, "Modifier", prof.Modifier.ToString());
            ini.WriteValue(section, "Key", prof.Key);
            return true;
        }

        public HotKeyProfile[] LoadHotKeys()
        {
            string[] sections = ini.GetSectionNames();
            HotKeyProfile[] profs = new HotKeyProfile[sections.Length];
            int i = 0;
            foreach (string section in sections)
            {
                int func = Convert.ToInt32(section.Replace("Function", ""));
                string mod = ini.GetString(section, "Modifier", "");
                if (mod == "Alt")
                {
                    profs[i] = new HotKeyProfile(HotKeyModifier.Alt,
                        ini.GetInt32(section, "Key", 0),
                        (iTunesFunctions)func);
                }
                else if (mod == "Control")
                {
                    profs[i] = new HotKeyProfile(HotKeyModifier.Control,
                        ini.GetInt32(section, "Key", 0),
                        (iTunesFunctions)func);
                }
                else if (mod == "Shift")
                {
                    profs[i] = new HotKeyProfile(HotKeyModifier.Shift,
                        ini.GetInt32(section, "Key", 0),
                        (iTunesFunctions)func);
                }
                else if (mod == "Windows")
                {
                    profs[i] = new HotKeyProfile(HotKeyModifier.Windows,
                        ini.GetInt32(section, "Key", 0),
                        (iTunesFunctions)func);
                }
                i++;
            }
            return profs;
        }
    }

    #region HotKeyProfile

    public struct HotKeyProfile
    {
        private Onyeyiri.HotKeyModifier mod;
        private int d_key;
        private iTunesFunctions func;

        public HotKeyProfile(Onyeyiri.HotKeyModifier m, int k, iTunesFunctions f)
        {
            mod = m;
            d_key = k;
            func = f;
        }

        public Onyeyiri.HotKeyModifier Modifier
        {
            get { return mod; }
        }

        public int Key
        {
            get { return d_key; }
        }

        public iTunesFunctions Function
        {
            get { return func; }
        }
    }

    #endregion
}
